package com.inter.insideinterservice;

import com.inter.entity.common.DeviceRealTimeInformation;
import com.inter.entity.pojo.Constant;
import com.inter.entity.pojo.request.RequestBody.QueryDeviceRealTimeRequestBody;
import com.inter.entity.pojo.request.RequestHeader;
import com.inter.entity.pojo.request.subRequest.QueryDeviceRealTimeRequest;
import com.inter.entity.pojo.response.subResponse.QueryDeviceRealTimeResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;

public class TurnToQueryDeviceRealTimeService {

    public DeviceRealTimeInformation queryDeviceRealTime(String deviceId, String deviceType, String secret, String partnerCode) {

        try {
            QueryDeviceRealTimeRequestBody body = new QueryDeviceRealTimeRequestBody();
            body.setDeviceId(deviceId);
            body.setDeviceType(deviceType);

            QueryDeviceRealTimeRequest request = new QueryDeviceRealTimeRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_queryDevice, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            //System.out.println("签名是：" + sign);
            request.setSign(sign);


            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QueryDeviceRealTimeRequest.class).toXml(request, "utf-8");
            //System.out.println("请求内容：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //System.out.println("返回结果：" + result);

            QueryDeviceRealTimeResponse responseObj = new JaxbUtil(QueryDeviceRealTimeResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                //System.out.println("==收到的是成功的返回：" + retCode);
                deviceId = responseObj.getBody().getDeviceId();
                deviceType = responseObj.getBody().getDeviceType();
                String otaFlag = responseObj.getBody().getOtaFlag();
                String bipFlag = responseObj.getBody().getBipFlag();
                String softSimIccid = responseObj.getBody().getSoftSimIccid();
                String softSimImsi = responseObj.getBody().getSoftSimImsi();
                String lifecycle = responseObj.getBody().getLifecycle();
                String lifecycleStartTime = responseObj.getBody().getLifecycleStartTime();
                String lifecycleEndTime = responseObj.getBody().getLifecycleEndTime();
                String lifecycleSlientPeriod = responseObj.getBody().getLifecycleSlientPeriod();
                String lifecycleShutdownPeriod = responseObj.getBody().getLifecycleShutdownPeriod();

                String orderCode = responseObj.getBody().getPackageOrder().getOrderCode();
                String packageName = responseObj.getBody().getPackageOrder().getPackageName();
                String packageCode = responseObj.getBody().getPackageOrder().getPackageCode();
                String packageType = responseObj.getBody().getPackageOrder().getPackageType();
                String buyType = responseObj.getBody().getPackageOrder().getBuyType();
                String orderPeriod = responseObj.getBody().getPackageOrder().getOrderPeriod();
                String flow = responseObj.getBody().getPackageOrder().getFlow();
                String usedFlow = responseObj.getBody().getPackageOrder().getUsedFlow();
                String flowUseRate = responseObj.getBody().getPackageOrder().getFlowUseRate();
                String orderUsedFlow = responseObj.getBody().getPackageOrder().getOrderUsedFlow();
                String orderDate = responseObj.getBody().getPackageOrder().getOrderDate();
                String activeDate = responseObj.getBody().getPackageOrder().getActiveDate();
                String expireDate = responseObj.getBody().getPackageOrder().getExpireDate();
                String isOrderRefuelingPackage = responseObj.getBody().getPackageOrder().getIsOrderRefuelingPackage();

                if(isOrderRefuelingPackage.equals("1")&&isOrderRefuelingPackage!=null){
                    String refPackageName = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefPackageName();
                    String refPackageCode = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefPackageCode();
                    String refPackageFlow = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefPackageFlow();
                    String refpackageOrderDate = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefpackageOrderDate();

                    DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                    information.setRetCode(retCode);
                    information.setResultInfo(resultInfo);
                    information.setDeviceId(deviceId);
                    information.setDeviceType(deviceType);
                    information.setOtaFlag(otaFlag);
                    information.setBipFlag(bipFlag);
                    information.setSoftSimIccid(softSimIccid);
                    information.setSoftSimImsi(softSimImsi);
                    information.setLifecycle(lifecycle);
                    information.setLifecycleStartTime(lifecycleStartTime);
                    information.setLifecycleEndTime(lifecycleEndTime);
                    information.setLifecycleSlientPeriod(lifecycleSlientPeriod);
                    information.setLifecycleShutdownPeriod(lifecycleShutdownPeriod);
                    information.setOrderCode(orderCode);
                    information.setPackageName(packageName);
                    information.setPackageCode(packageCode);
                    information.setPackageType(packageType);
                    information.setBuyType(buyType);
                    information.setOrderPeriod(orderPeriod);
                    information.setFlow(flow);
                    information.setUsedFlow(usedFlow);
                    information.setFlowUseRate(flowUseRate);
                    information.setOrderUsedFlow(orderUsedFlow);
                    information.setOrderDate(orderDate);
                    information.setActiveDate(activeDate);
                    information.setExpireDate(expireDate);
                    information.setIsOrderRefuelingPackage(isOrderRefuelingPackage);
                    information.setRefPackageName(refPackageName);
                    information.setRefPackageCode(refPackageCode);
                    information.setRefPackageFlow(refPackageFlow);
                    information.setRefpackageOrderDate(refpackageOrderDate);
                    return information;

                }else{
                    //System.out.println("走这边的对吧？");
                    DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                    information.setRetCode(retCode);
                    information.setResultInfo(resultInfo);
                    information.setDeviceId(deviceId);
                    information.setDeviceType(deviceType);
                    information.setOtaFlag(otaFlag);
                    information.setBipFlag(bipFlag);
                    information.setSoftSimIccid(softSimIccid);
                    information.setSoftSimImsi(softSimImsi);
                    information.setLifecycle(lifecycle);
                    information.setLifecycleStartTime(lifecycleStartTime);
                    information.setLifecycleEndTime(lifecycleEndTime);
                    information.setLifecycleSlientPeriod(lifecycleSlientPeriod);
                    information.setLifecycleShutdownPeriod(lifecycleShutdownPeriod);
                    information.setOrderCode(orderCode);
                    information.setPackageName(packageName);
                    information.setPackageCode(packageCode);
                    information.setPackageType(packageType);
                    information.setBuyType(buyType);
                    information.setOrderPeriod(orderPeriod);
                    information.setFlow(flow);
                    information.setUsedFlow(usedFlow);
                    information.setFlowUseRate(flowUseRate);
                    information.setOrderUsedFlow(orderUsedFlow);
                    information.setOrderDate(orderDate);
                    information.setActiveDate(activeDate);
                    information.setExpireDate(expireDate);
                    information.setIsOrderRefuelingPackage(isOrderRefuelingPackage);
                    return information;

                }

            } else {
                //System.out.println("==收到的是不成功的返回：" + retCode);
                //也要把结果信息返回回去
                DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                information.setRetCode(retCode);
                information.setResultInfo(resultInfo);
                return information;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
